package org.syx.droid.telnet.test;

import android.widget.TextView;

/**
 * A job that appends text to the specified TextView
 * 
 * @author cesardepadua
 * 
 */
public class TextAppender implements Runnable {
	
	/**
	 * The text view to append `appendedMessage` to.
	 */
	private final TextView textView;
	
	/**
	 * The message/string to append to the designated TextView.
	 */
	private final String message;
	
	private TextAppender (TextView t, String m) {
	
		this.textView = t;
		this.message = m;
	}
	
	@Override
	public void run () {
	
		textView.append (this.message);
	}
	
	/**
	 * A singleton factory for creating {@link TextAppender} instances.
	 * 
	 * @author cesardepadua
	 * 
	 */
	public static class Factory {
		/**
		 * The {@link TextView} to append messages to.
		 */
		private TextView textView = null;
		
		/**
		 * The single instance of this {@link Factory}
		 */
		private static TextAppender.Factory instance = null;
		
		/**
		 * The default contructor for {@link Factory}.
		 * 
		 * The method for getting the factory is through {@link Factory#getInstance()}.
		 * 
		 * @param t
		 *            The {@link TextView} to append to.
		 */
		private Factory (TextView t) {
		
			this.textView = t;
		}
		
		/**
		 * This initializes the factory, and sets the {@link TextView} to be
		 * written to by its created products.
		 * 
		 * This <b>MUST</b> be called prior to {@link Factory#getInstance ()}
		 * 
		 * @param textView
		 *            The {@link android.widget.TextView TextView} that the created {@link TextAppender}
		 *            instances will write to.
		 * @return An instance of the factory.
		 */
		public static synchronized TextAppender.Factory initialize (
				TextView textView) {
		
			if (instance == null)
				instance = new Factory (textView);
			
			return instance;
		}
		
		/**
		 * Change the TextView to append to.
		 * 
		 * @param textView
		 *            The Text View to append to.
		 * @return An instance of the factory.
		 */
		public synchronized TextAppender.Factory setTextView (TextView textView) {
		
			this.textView = textView;
			return this;
		}
		
		/**
		 * Create a {@link TextAppender} instance that will append `message` to
		 * the {@link android.widget.TextView TextView} held by the factory.
		 * 
		 * @param message
		 *            The message to be appended.
		 * @return An instance of the newly created         {@TextAppender
		 * }.
		 */
		public synchronized TextAppender createJob (String message) {
		
			return new TextAppender (this.textView, message);
		}
		
		/**
		 * 
		 * @return true, if the {@link TextAppender.Factory} instance has been
		 *         initialized using {@link TextAppender.Factory#initialize}. false, if
		 *         otherwise.
		 */
		public boolean isInitialized () {
		
			return (this.textView != null);
		}
		
		/**
		 * 
		 * @return The instance of {@link TextAppender.Factory}.
		 * @throws IllegalStateException
		 *             Thrown if {@link #initialize(TextView)} has not yet been
		 *             called.
		 */
		public static synchronized TextAppender.Factory getInstance ()
				throws IllegalStateException {
		
			if (instance == null)
				throw new IllegalStateException (
						"The instance of the Factory has not been initialized! Please ensure that `initialize ()` has been called.");
			else
				return instance;
		}
	}
	
}
